package com.example.myproject.zebra.demo2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;

public class SimplePrint {

	byte[] dotfont;

	/**
	 * 读取指令txt文件
	 * @return
	 * @throws Exception
	 */
	public static String input() throws Exception {
		File inputStream = new File("e:/C.txt");
		InputStreamReader read = new InputStreamReader(new FileInputStream(
				inputStream));
		BufferedReader reader = new BufferedReader(read);
		String string = "";
		StringBuffer buffer = new StringBuffer();
		while ((string = reader.readLine()) != null) {
			buffer.append(string);
		}
		read.close();

		return buffer.toString();
	}

	public static void main(String[] args) {
		try {
			SimplePrint a = new SimplePrint();
			String str = input();
			a.print(str);//将打印指令发送到打印机
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public SimplePrint() throws Exception {
		File file = new File("d:/ts24.lib");
		FileInputStream fis = new FileInputStream(file);
		dotfont = new byte[fis.available()];
		fis.read(dotfont);
		fis.close();
	}

	public void print(String str) throws PrintException {
		PrintService psZebra = PrintServiceLookup.lookupDefaultPrintService();
		if (psZebra == null) {
			return;
		}
		DocPrintJob job = psZebra.createPrintJob();
		byte[] by = str.getBytes();
		DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
		Doc doc = new SimpleDoc(by, flavor, null);
		job.print(doc, null);
	}

}